package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Evaluation;
import ex.com.jm.food.service.UserEvalService;
import ex.com.jm.food.service.UserMenuService;
import ex.com.jm.food.utils.JsonUtils;
/**
 * 
 * @author zhaojw
 *
 */
public class UserEvalAction extends ActionSupport {


	private static final long serialVersionUID = 1L;
	
	private Evaluation  evaluation;
	private UserEvalService userEvalService;
	private UserMenuService userMenuService;
	private String menuId;
	private String comId;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private Map<String, String> company=new HashMap<String, String>();
	private List<Evaluation> evaluations=new ArrayList<Evaluation>();
	public Evaluation getEvaluation() {
		return evaluation;
	}
	public void setEvaluation(Evaluation evaluation) {
		this.evaluation = evaluation;
	}
	public UserEvalService getUserEvalService() {
		return userEvalService;
	}
	public void setUserEvalService(UserEvalService userEvalService) {
		this.userEvalService = userEvalService;
	}
	public String getMenuId() {
		return menuId;
	}
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public List<Evaluation> getEvaluations() {
		return evaluations;
	}
	public void setEvaluations(List<Evaluation> evaluations) {
		this.evaluations = evaluations;
	}
	public UserMenuService getUserMenuService() {
		return userMenuService;
	}
	public void setUserMenuService(UserMenuService userMenuService) {
		this.userMenuService = userMenuService;
	}
	public Map<String, String> getCompany() {
		return company;
	}
	public void setCompany(Map<String, String> company) {
		this.company = company;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	/**
	 * 列表页面
	 * @return
	 */
	public String evalList(){
		int offset=(initPage-1)*pageRow;
		evaluations=this.getUserEvalService().getEvals(comId, offset, pageRow);
		List<Map<String, String>> companys=this.getUserMenuService().getCompany(comId);
		 if(companys!=null){
			 company=companys.get(0);
		 }
		return "evalList";
	}
	/**
	 * 数据
	 */
	public void getEvals(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<Evaluation> evaluations=this.getUserEvalService().getEvals(menuId, offset, pageRow);
		JSONArray ja=JSONArray.fromObject(evaluations);
		totalCount=this.getUserEvalService().evalCount(menuId);
		JSONObject jo=JsonUtils.buildJson(ja, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out=response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 评价
	 * @return
	 */
	public String saveEval(){
		this.getUserEvalService().saveEval(evaluation);
		return "list";
	}

}
